package cn.com.anysdk.starter.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 基于Spring AOP的切面类，拦截ISmsService接口所有方法，
 * 在SMS操作前后自动记录操作日志。
 */
@Aspect
@Component
public class SmsOperationLogAspect {

    private static final Logger logger = LoggerFactory.getLogger(SmsOperationLogAspect.class);

    /**
     * 切点：拦截ISmsService接口的所有方法
     */
    @Pointcut("execution(* cn.com.anysdk.sms.api.ISmsService.*(..))")
    public void smsServiceMethods() {}

    /**
     * 环绕通知：在SMS操作前后记录日志
     */
    @Around("smsServiceMethods()")
    public Object logSmsOperation(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().toShortString();
        logger.info("[SMS操作] 开始执行方法: {}", methodName);
        Object result;
        try {
            result = joinPoint.proceed();
            logger.info("[SMS操作] 方法执行成功: {}", methodName);
        } catch (Throwable ex) {
            logger.error("[SMS操作] 方法执行异常: {}", methodName, ex);
            throw ex;
        }
        logger.info("[SMS操作] 结束执行方法: {}", methodName);
        return result;
    }
}
